# -*- coding: utf-8 -*-
"""
Created on Sun Apr 25 10:52:13 2021

@author: zhangfan
"""

from PIL import Image
import os

File=os.listdir(".")
Result=[]
for i in File:
    if ".png" in i or ".jpg" in i or ".jpeg" in i or ".bmp" in i or ".gif" in i :
        Result.append(i)
        
mode=input("定宽度批处理请输入0，定高度批处理请输入1（默认为定宽度处理）！\n>>")
Size=input("请输入图片限定值大小（默认为1000）！\n>>")

if mode == "":
    mode="0"
if Size == "":
    Size="1000"
if not os.path.exists(".\\Result"):
    os.makedirs(".\\Result")  
imgnum=0
for img in Result:
    imgnum +=1 
    outfile = ".\\Result\\"+str(imgnum)+".png"
    im = Image.open(img)
    (x,y) = im.size #read image size
    if int(mode)==1 :   #定高度模式
        Y=int(Size)
        X=int(x*Y/y)
    else:     #定宽度模式
        X=int(Size)
        Y=int(y*X/x)
    out = im.resize((X,Y),Image.ANTIALIAS) #resize image with high-quality
    out.save(outfile)